package com.ayu.gulimall.product.service.impl;

import com.ayu.gulimall.product.dao.SkuImagesDao;
import com.ayu.gulimall.product.dao.SkuSaleAttrValueDao;
import com.ayu.gulimall.product.entity.SkuImagesEntity;
import com.ayu.gulimall.product.entity.SkuSaleAttrValueEntity;
import com.ayu.gulimall.product.vo.SkuDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ayu.gulimall.product.dao.SkuInfoDao;
import com.ayu.gulimall.product.entity.SkuInfoEntity;
import com.ayu.gulimall.product.service.SkuInfoService;
import com.ayu.common.utils.PageUtils;
import com.ayu.common.utils.Query;

@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {


    @Autowired
    SkuInfoDao skuInfoDao;

    @Autowired
    SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    SkuImagesDao skuImagesDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuDetailVO getSkuDetailVOById(Long skuId) {
        //1、添加商品基本信息
        SkuInfoEntity skuInfoEntity = skuInfoDao.selectById(skuId);

        //2、商品的图片列表
        List<SkuImagesEntity> imgList = skuImagesDao.selectList(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));

        //3、商品的颜色和规格

        List<String> colorList = skuSaleAttrValueDao.selectList(new QueryWrapper<SkuSaleAttrValueEntity>()
                .eq("sku_id", skuId).eq("attr_id",1))
                .stream().map(item -> item.getAttrValue()).collect(Collectors.toList());

        List<String> standardList = skuSaleAttrValueDao.selectList(new QueryWrapper<SkuSaleAttrValueEntity>()
                .eq("sku_id", skuId).eq("attr_id", 2))
                .stream().map(item -> item.getAttrValue()).collect(Collectors.toList());

        SkuDetailVO skuDetailVO =new SkuDetailVO();
        skuDetailVO.setSkuInfo(skuInfoEntity);
        skuDetailVO.setImgList(imgList);
        skuDetailVO.setColorList(colorList);
        skuDetailVO.setStandardList(standardList);

        return skuDetailVO;
    }


}